package com.superbytecode.sample.redis.controller;

import com.superbytecode.sample.redis.entity.RedisIn;
import com.superbytecode.sample.redis.entity.User;
import com.superbytecode.sample.redis.service.RedisLockService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: superbytecode-beacon
 * @Package: com.superbytecode.sample.controller
 * @ClassName: RedisLockController
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2023/11/21 16:26
 * @Version: V1.0
 * @Copyright: 2023 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@RestController
@RequestMapping("/com/superbytecode/sample/redis/lock")
public class RedisLockController {
    @Resource
    private RedisLockService redisLockService;

    /**
     * 锁一个参数
     *
     * @param redisIn key
     */
    @PostMapping("/singleKey")
    public int singleKey(@RequestBody RedisIn redisIn) throws InterruptedException {
        String key = redisIn.getKey();
        return redisLockService.singleKeyLock(key);
    }

    /**
     * 锁两个参数
     *
     * @param redisIn keys
     */

    @PostMapping("/multipleKey")
    public int lock2(@RequestBody RedisIn redisIn) throws InterruptedException {
        String key1 = redisIn.getKey1();
        String key2 = redisIn.getKey2();
        return redisLockService.multipleKeyLock(key1, key2);
    }

    /**
     * 锁入参对象中的属性
     * 此处示例锁用户id
     */
    @PostMapping("/objectKey")
    public int objectKey(@RequestBody User user) throws InterruptedException {
        return redisLockService.objectKey(user);
    }

}